package main

import (
	"fmt"
	"math/rand"
	"time"
	"strconv"
)

func putNum(numChan chan int) {

	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 24; i++ {
		num := rand.Intn(10)
		numChan <- num
	}
	close(numChan)
}

func readNum(numChan chan int, exitChan chan bool) {

	for {
		num, ok := <- numChan
		if !ok {
			exitChan <- true
			break
		}
		for i := num; i > 0; i-- {
			sum := 0
			for j := 1; j <= i; j++ {
				sum += j
			}
			fmt.Println("res[" + strconv.Itoa(i) + "]=" + strconv.Itoa(sum))
		}
	}

	exitChan <- true

}

func main() {

	var numChan chan int
	numChan = make(chan int, 24)
	var exitChan chan bool
	exitChan = make(chan bool, 8)

	go putNum(numChan)
	for i := 0; i < 8; i++ {
		go readNum(numChan, exitChan)
	}

	go func() {
		for i := 0; i < 8; i ++ {
			<- exitChan
		}
		close(exitChan)
	} ()

	time.Sleep(1)
}